package com.ysfxt.controller;


import com.ysfxt.model.SysTeacher;
import com.ysfxt.result.Result;
import com.ysfxt.service.SysTeacherService;
import com.ysfxt.vo.TeacherParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ysfxt
 * @since 2024-03-29
 */
@RestController
@RequestMapping("/api/sys-teacher")
public class SysTeacherController {
    @Resource
    private SysTeacherService sysTeacherService;
    
    /**
     * 条件查询
     * @param teacherParam
     * @return
     */
    @PostMapping("selectTeacher")
    public Result selectTeacher(@RequestBody TeacherParam teacherParam){
        return sysTeacherService.select(teacherParam);
    }
    
    /**
     * 账号新增
     * @param sysTeacher
     * @return
     */
    @PostMapping("save")
    public Result saveTeacher(@RequestBody SysTeacher sysTeacher){
        return sysTeacherService.saveTeacher(sysTeacher);
    }
    
    /**
     * 根据id查询账号信息
     * @param id
     * @return
     */
    @GetMapping("getTeacherInfo/{id}")
    public Result getTeacherInfo(@PathVariable Long id){
        SysTeacher sysTeacher = sysTeacherService.getById(id);
        sysTeacher.setIdentityString(sysTeacher.getIdentity().getComment());
        sysTeacher.setScopeString(sysTeacher.getScope().getComment());
        return Result.ok(sysTeacher);
    }
    
    /**
     * 更新
     * @param sysTeacher
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody SysTeacher sysTeacher){
        sysTeacher.setGmtModified(new Date());
        if(sysTeacherService.updateById(sysTeacher)){
            return Result.ok();
        }else {
            return Result.fail("更新失败，请重试。");
        }
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Long id){
        if(sysTeacherService.removeById(id)){
            return Result.ok();
        }else{
            return Result.fail("删除失败，请重试。");
        }
    }
    
    /**
     * 导出
     * @param response
     */
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response){
        sysTeacherService.exportData(response);
    }
    
    /**
     * 导入
     * @param file
     */
    @PostMapping("/importData")
    public void importData(MultipartFile file){
        sysTeacherService.importData(file);
    }
}

